import { fileURLToPath, URL } from 'node:url';

import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueDevTools from 'vite-plugin-vue-devtools';
import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import { execSync } from 'node:child_process';
// import { viteMockServe } from 'vite-plugin-mock';



import packageJson from './package.json'

const getGitCommitHash = (): string => {
  try {
    return execSync('git rev-parse --short HEAD').toString().trim();
  } catch (err) {
    console.error('Error getting Git commit', err);
    return 'unknown';
  }
}


// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    Components({
      resolvers: [
        AntDesignVueResolver({
          importStyle: false, // css in js
        }),
      ]
    }),
    // viteMockServe({
    //   mockPath: 'mock',
    //   enable: false,
    //   ignore: /^\_/,
    //   logger: true,
    // })
  ],
  server: {
    host: true,
    port: 5137,
    open: true,
    proxy: {
      '/api': {
        target: 'http://localhost:8080/',
        changeOrigin: true,
        secure: false,
      },
    },
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  define: {
    'process.env': loadEnv(process.env.mode as string, process.cwd()),
    VITE_APP_VERSION: `"${packageJson.version}"`,
    VITE_BUILD_DATE: JSON.stringify(new Date().toLocaleDateString()),
    VITE_GIT_COMMIT_HASH: JSON.stringify(getGitCommitHash()),
  }
})
